package cn.hinglo.gameclaw.im.service;

import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.ResultUtils;
import cn.hinglo.gameclaw.im.bean.ChatRoom;
import cn.hinglo.gameclaw.im.dao.ChatRoomDao;
import cn.hinglo.gameclaw.im.utils.HttpClientUtils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static cn.hinglo.gameclaw.im.utils.HttpClientUtils.getCode;
import static cn.hinglo.gameclaw.im.utils.HttpClientUtils.getResult;


/**
 * @createTime: 2018/3/6
 * @author: HingLo
 * @description: 房间管理的服务层
 */
@Service
public class ChatRoomService {
    private static final String URL = "https://api.netease.im/nimserver/";
    private final ChatRoomDao chatRoomDao;

    @Autowired
    public ChatRoomService(ChatRoomDao chatRoomDao) {
        this.chatRoomDao = chatRoomDao;
    }

    public static void main(String[] args) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("creator", "001");
        param.put("name", "room001");

        String result = HttpClientUtils.postFormData(URL + "chatroom/create.action", param, false);
        System.out.println(result);

    }

    /**
     * 查询聊天室房间
     *
     * @param roomId 房间的Id
     * @return 房间的信息
     */
    public Result queryRoomInfoService(Long roomId) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("roomid", roomId);
        String result = HttpClientUtils.postFormData(URL + "chatroom/get.action", param, false);
        if (getCode(result)) {
            return ResultUtils.success(getResult(result, "chatroom"));
        }
        return ResultUtils.error("聊天室信息不存在");
    }

    /**
     * 创建聊天室
     *
     * @param creator 创建人
     * @param name    聊天室的名称
     * @return 创建结果
     */
    public Result createRoomService(String creator, String name) {
        if (StrUtil.isNotBlank(creator) && StrUtil.isNotBlank(name)) {
            Map<String, Object> param = new HashMap<>(2);
            param.put("creator", creator);
            param.put("name", name);

            String result = HttpClientUtils.postFormData(URL + "chatroom/create.action", param, false);
            if (getCode(result)) {
                //解析对象，将结果保存到数据库中
                Object res = getResult(result, "chatroom");
                ChatRoom chatroom = JSONUtil.toBean(JSONUtil.parseObj(res), ChatRoom.class);
                chatRoomDao.insert(chatroom);

                return ResultUtils.success(res);
            }
        }
        return ResultUtils.error("创建聊天室失败，参数传递不争取");
    }

    /**
     * 创建聊天室,返回聊天室信息
     *
     * @param creator 创建人
     * @param name    聊天室的名称
     * @return 创建结果
     */
    public ChatRoom createRoomBeanService(String creator, String name) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("creator", creator);
        param.put("name", name);

        String result = HttpClientUtils.postFormData(URL + "chatroom/create.action", param, false);
        System.out.println(result);
        if (getCode(result)) {
            //解析对象，将结果保存到数据库中
            Object res = getResult(result, "chatroom");
            return JSONUtil.toBean(JSONUtil.parseObj(res), ChatRoom.class);
        }
        return null;
    }

    /**
     * 保存聊天室
     *
     * @param chatRoom 聊天室的实体
     * @return 是否创建成功
     */
    public boolean saveService(ChatRoom chatRoom) {
        return chatRoomDao.insert(chatRoom) == 1;
    }

    /**
     * @param roomId       更新聊天室的信息
     * @param name         名字
     * @param announcement 聊天室的公告
     * @return 更新结果
     */
    public Result updateRoomService(long roomId, String name, String announcement) {

        if (roomId < 1) {
            return ResultUtils.error("聊天室的Id不正确");
        }
        Map<String, Object> param = new HashMap<>(3);
        param.put("roomid", roomId);
        if (StrUtil.isNotBlank(name)) {
            param.put("name", name);
        }
        if (StrUtil.isNotBlank(announcement)) {
            param.put("announcement", announcement);
        }
        String result = HttpClientUtils.postFormData(URL + "chatroom/update.action", param, false);
        if (getChartRoomBean(result)) {
            return ResultUtils.success("更新成功");
        }
        return ResultUtils.error("更新失败");
    }

    public Result updateRoomStatusService(long roomId, String operator, boolean valid) {
        Map<String, Object> param = new HashMap<>(3);
        param.put("roomid", roomId);
        param.put("operator", operator);
        param.put("valid", valid);
        String result = HttpClientUtils.postFormData(URL + "chatroom/update.action", param, false);
        if (getChartRoomBean(result)) {
            return ResultUtils.success("更新成功");
        }

        return ResultUtils.error("聊天室状态更新失败");

    }

    /**
     * 通过json格式的字符串来获取聊天的对象信息并更新到本地的数据库
     *
     * @param result json格式的数据
     * @return 更新结果
     */
    private boolean getChartRoomBean(String result) {
        if (getCode(result)) {
            Object res = getResult(result, "chatroom");
            ChatRoom chatroom = JSONUtil.toBean(JSONUtil.parseObj(res), ChatRoom.class);
            chatRoomDao.updateByPrimaryKey(chatroom);
            return true;
        }
        return false;
    }
}
